-- only one textbox can have focus at a time. 
focusTextbox = {}   -- focused textbox intercepts all keyboard input
canClick = true     -- for events only on mouse release

dragx1 = -1
dragy1 = -1
dragx2 = -1
dragy2 = -1

function createGUIcontroller()
	local t = {}

	t.buttons = {}
	t.textboxes = {}
	t.focusTextbox = {}
	
	return t
end

function love.mousepressed(x, y, button)
	focusTextbox = {}
	
	if button == 'l' and love.mouse.getY() < 600 and canClick == true then
		canClick = false
		dragx1 = love.mouse.getX()
		dragy1 = love.mouse.getY()
	end
end

function love.mousereleased(x, y, button)
    canClick = true
    -- Left Mouse Button ==> Create Square
    if button == 'l' and y < 700 then
        if (dragx1 ~= -1 and dragy1 ~= -1) then
			dragx2 = love.mouse.getX()
			dragy2 = love.mouse.getY()
			local topLeftX= math.min(dragx1, dragx2)
			local topLeftY= math.min(dragy1, dragy2)
			createSquare(topLeftX, topLeftY, math.max(dragx1, dragx2)-topLeftX, math.max(dragy1, dragy2)-topLeftY)
        end
        dragx1 = -1;    dragy1 = -1;
		dragx2 = -1;    dragy2 = -1
    -- Right Mouse Button ==> Create Start or End
    elseif button == 'r' and y < 700 then
        
    -- Middle Mouse Button ==> Destroy object
    elseif button == 'm' then
        -- Delete Square if you clicked on a square
        for i,v in ipairs(terrain) do
            if ((v.x < x  and x < v.x+v.w) and (v.y < y and y < v.h + v.y)) then 
                explosion1(x, y)
				table.remove(terrain, i)
            end
        end
    end 
end

function love.keypressed(key,unicode)
	if focusTextbox.text ~= nil then
		if key == 'backspace' then
			focusTextbox.text = string.sub(focusTextbox.text,1, string.len(focusTextbox.text)-1)
		elseif string.len(key) == 1 then --if its not an alpha num key, itll be longer than 1 -  'shift' etc
			focusTextbox.text = focusTextbox.text..key
		end
        
        if focusTextbox.name == "Intensity" then
            intensity = tonumber(focusTextbox.text)
        elseif focusTextbox.name == "Starting Angle" then
            angleText = tonumber(focusTextbox.text)
        end 
        
        if selectedRobot ~= nil then 
            if focusTextbox.name == "K11: " then
                if tonumber(focusTextbox.text) == nil then 
                    selectedRobot.k11 = 0
                else 
                    selectedRobot.k11 = tonumber(focusTextbox.text)
                end
            elseif focusTextbox.name == "K12: " then
                if tonumber(focusTextbox.text) == nil then 
                    selectedRobot.k12 = 0
                else 
                    selectedRobot.k12 = tonumber(focusTextbox.text)
                end
            elseif focusTextbox.name == "K21: " then
                if tonumber(focusTextbox.text) == nil then 
                    selectedRobot.k21 = 0
                else 
                    selectedRobot.k21 = tonumber(focusTextbox.text)
                end
            elseif focusTextbox.name == "K22: " then
                if tonumber(focusTextbox.text) == nil then 
                    selectedRobot.k22 = 0
                else 
                    selectedRobot.k22 = tonumber(focusTextbox.text)
                end
            end
        end 
	end
end

function createButton(parent,x,y,h,name,func)
local t = {}
	t.name = name
	t.func = func
	
	t.x=x
	t.y=y
	t.w= string.len(name) * 8
	t.h=h
	
	table.insert(parent.buttons,t)
    
    return t
end

function createTextbox(parent,x,y,w,h,name,text)
	local t = {}
	
	if text == nil then
		t.text = ""
	else
		t.text = text
	end
	
	t.name = name
	
	t.x=x
	t.y=y
	t.w=w
	t.h=h
	
	table.insert(parent.textboxes,t)
    
    return t
end

function handleTextBoxes(ctrl)
	for i,v in ipairs(ctrl.textboxes) do
		if love.mouse.getX() > v.x and love.mouse.getX() < v.x + v.w and love.mouse.getY() > v.y and love.mouse.getY() < v.y + v.h and love.mouse.isDown("l") then
			focusTextbox = v	
		end
		if v.name == "Angle" then
			angleText = tonumber(v.text)
		end
	end
end

function handleButtons(ctrl)
	for i,v in ipairs(ctrl.buttons) do
		if love.mouse.getX() > v.x and love.mouse.getX() < v.x + v.w and love.mouse.getY() > v.y and love.mouse.getY() < v.y + v.h and love.mouse.isDown("l") and canClick == true then
			v.func(ctrl)	
			canClick = false
		end
	end
end

function drawTextBoxes(ctrl)
	for i,v in ipairs(ctrl.textboxes) do
		love.graphics.rectangle("line",v.x, v.y, v.w, v.h)
        if v.text ~= nil then 
            love.graphics.print(v.text,v.x + 3,v.y + 1,0,1,1)
        else
            love.graphics.print("",v.x + 3,v.y + 1,0,1,1)
        end 
		love.graphics.print(v.name,v.x - string.len(v.name) * 8,v.y,0,1,1)
	end
	
	if focusTextbox.x ~= nil then
		love.graphics.setColor(255,255,0)
		love.graphics.rectangle("line",focusTextbox.x, focusTextbox.y, focusTextbox.w, focusTextbox.h)
		love.graphics.setColor(255,255,255)
	end

	for i,v in ipairs(ctrl.buttons) do
		
		if v.name == "Clear Path" or v.name == "Clear Squares" then
			love.graphics.setColor(255,100,100)
		elseif v.name == "Draw Path" and pathRunning == true then
			love.graphics.setColor(math.random(255),math.random(255),math.random(255))
		else
			love.graphics.setColor(100,255,100)
		end
		love.graphics.rectangle("fill",v.x, v.y, string.len(v.name) * 8, v.h)
		love.graphics.setColor(0,0,0)
		love.graphics.print(v.name,v.x + 3,v.y + 1,0,1,1)
		love.graphics.setColor(255,255,255)
	end	
end

function drawGUI(ctrl)
	--draw particles
	love.graphics.setColorMode("modulate")
	love.graphics.setBlendMode("additive")
	love.graphics.setBlendMode("alpha")

	--draw background box of gui
	love.graphics.setColor(80,80,80)
	love.graphics.rectangle("fill",0, 590, 800, 200)
	love.graphics.setColor(255,255,255)
	
	--draw textboxes and buttons
	drawTextBoxes(ctrl)	
end